package com.basic.business.rpaServer;

import com.basic.business.common.constants.Constants;
import com.basic.business.common.enums.PstStatus;
import com.basic.business.common.redis.RedisCacheUtils;
import com.basic.business.common.utils.StringUtils;
import com.basic.business.common.utils.kmUtil.KmTagsUtils;
import com.basic.business.log.service.ITOrdersLogService;
import com.basic.business.order.domain.TOrdersDetail;
import com.basic.business.order.domain.TOrdersMain;
import com.basic.business.order.domain.TOrdersStatus;
import com.basic.business.order.service.ITOrdersMainService;
import com.basic.business.order.service.ITOrdersStatusService;
import com.basic.business.redis.domain.TOrdersMainRedis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 印刷品处理
 */
@Slf4j
@Component
public class HandlePrint {

    @Resource
    private ITOrdersMainService mainService;

    @Resource
    private ITOrdersStatusService statusService;

    @Resource
    private ITOrdersLogService logService;

    public boolean init(List<TOrdersDetail> detailCollect, TOrdersMain main){
        long mainSid = main.getSid();

        TOrdersStatus status = TOrdersStatus.createTOrdersStatus(main);

        try {
            // 获取印刷品集合
            List<String> printList = RedisCacheUtils.getCacheObject(Constants.PRINTS_REDIS_KEY);
            if (printList.isEmpty()) {
                return false;
            }
            // 是否全是印刷品
            boolean isAllPrint = detailCollect.stream().allMatch(detail -> printList.stream().anyMatch(code -> StringUtils.contains(detail.getOuterSkuId(), code)));
            // 如果全是印刷品，是则新增印刷品标签，删除不做处理标签，删除正常处理标签，否则删除印刷品标签
            if (isAllPrint) {
                // 获取Redis信息
                TOrdersMainRedis mainRedis = RedisCacheUtils.getCacheObject(Constants.ORDERS_REDIS_KEY + mainSid);

                // 更新子订单自动处理状态为 成功
                mainRedis.getDetailList().forEach(detail -> detail.setPst(PstStatus.AUTO_FINISHED.name()));

                RedisCacheUtils.setCacheObject(Constants.ORDERS_REDIS_KEY + mainSid, mainRedis);

                // 更新标签
                String tagsStr = KmTagsUtils.updateExclusiveTag(mainSid, main.getTags(), KmTagsUtils.TAG_PRINTED);

                // 更新主订单自动处理状态为【成功】
                status.setOrderStatus(PstStatus.AUTO_FINISHED.name());
                statusService.insertPst(status);
                // 写日志
                logService.insert(mainSid, mainSid, String.format("执行【印刷品】策略成功；%s", tagsStr));

                return true;
            }
        } catch (Exception e) {
            // 更新标签
            String tagsStr = KmTagsUtils.updateExclusiveTag(mainSid, main.getTags(), KmTagsUtils.TAG_NORMAL_PROCESSING);
            // 更新主订单自动处理状态为【人工处理】
            status.setOrderStatus(PstStatus.MANUAL.name());
            statusService.insertPst(status);
            // 写日志
            logService.insert(mainSid, mainSid, String.format("执行【印刷品】策略失败；%s", tagsStr));
            log.error("执行【印刷品】策略失败；系统单号：{}；原因：", mainSid, e);
            return false;
        }
        return false;
    }

}
